<template>
	<view class="recharge-container">
		<!-- 新增充值界面 -->
		<!-- 余额卡片 -->
		<view class="balance-card">
			<view class="balance-icon">
				<uni-icons type="wallet" size="32" color="#FFFFFF"></uni-icons>
			</view>
			<view class="balance-info">
				<text class="balance-label">当前余额</text>
				<view class="balance-value-wrapper">
					<text class="balance-value" v-if="!isLoading">¥{{userBalance}}</text>
					<text class="balance-loading" v-else>加载中...</text>
				</view>
			</view>
			<view class="refresh-btn" @tap="refreshBalance">
				<uni-icons type="reload" size="24" color="#FFFFFF"></uni-icons>
			</view>
		</view>
		
		<!-- 充值金额选择 -->
		<view class="section-title">选择充值金额</view>
		<view class="amount-options">
			<view 
				v-for="(item, index) in amountOptions" 
				:key="index" 
				class="amount-item" 
				:class="{ 'active': selectedAmount === item.value }"
				@tap="selectAmount(item.value)"
			>
				<text class="amount-value">{{item.label}}</text>
			</view>
		</view>
		
		<!-- 自定义金额输入 -->
		<view class="custom-amount">
			<view class="custom-title">自定义金额</view>
			<view class="custom-input-wrapper">
				<text class="currency-symbol">¥</text>
				<input 
					type="digit" 
					class="custom-input" 
					placeholder="请输入充值金额" 
					:value="customAmount"
					@input="onCustomAmountInput"
				/>
			</view>
		</view>
		
		<!-- 支付方式选择 -->
		<view class="section-title">选择支付方式</view>
		<view class="payment-methods">
			<view 
				v-for="(method, index) in paymentMethods" 
				:key="index" 
				class="payment-item"
				:class="{ 'active': selectedPaymentMethod === method.id }"
				@tap="selectPaymentMethod(method.id)"
			>
				<view class="payment-icon">
					<image :src="method.icon" mode="aspectFit" />
				</view>
				<view class="payment-info">
					<text class="payment-name">{{method.name}}</text>
					<text class="payment-desc">{{method.desc}}</text>
				</view>
				<view class="payment-selected" v-show="selectedPaymentMethod === method.id">
					<uni-icons type="checkmarkempty" size="18" color="#FFFFFF" />
				</view>
			</view>
		</view>
		
		<!-- 充值说明 -->
		<view class="recharge-notes">
			<text class="notes-title">充值说明：</text>
			<text class="notes-item">• 充值金额最低10元</text>
			<text class="notes-item">• 余额可用于下单、购买商品</text>
			<text class="notes-item">• 余额无使用期限，长期有效</text>
			<text class="notes-item">• 充值成功后，余额将立即到账</text>
		</view>
		
		<!-- 确认按钮 -->
		<view class="confirm-btn-wrapper">
			<button 
				class="confirm-btn" 
				:class="{ 'disabled': !canConfirm }"
				@tap="confirmRecharge"
			>
				确认充值
			</button>
		</view>
	</view>
</template>

<script>
	import { tip, loading, hideloading } from '../../utils';
	import { getInfo, rechargeApi, payRechargeApi } from '../../api/user';
	import { userStore } from '../../store/userStore';
	import uniIcons from '@/uni_modules/uni-icons/components/uni-icons/uni-icons.vue';
	const user = userStore(); // 初始化用户状态
	// console.log(user);
	export default {
		components: {
			uniIcons
		},
		data() {
			return {
				//userBalance: 0, // 用户当前余额
				selectedAmount: 50, // 默认选择50元
				customAmount: '', // 自定义金额
				selectedPaymentMethod: 'wechat', // 默认微信支付
				isLoading: false, // 是否正在加载数据
				amountOptions: [
					{
						value: 10,
						label: '¥10'
					},
					{
						value: 50,
						label: '¥50'
					},
					{
						value: 100,
						label: '¥100'
					},
					{
						value: 200,
						label: '¥200'
					},
					{
						value: 500,
						label: '¥500'
					},
					{
						value: 1000,
						label: '¥1000'
					}
				],
				paymentMethods: [
					{
						id: 'wechat',
						name: '微信支付',
						icon: 'https://ts1.tc.mm.bing.net/th/id/R-C.692de9abd94bc6a408ce5591e04403b1?rik=Pdt3a4ecCmrvQQ&riu=http%3a%2f%2fpic.ntimg.cn%2ffile%2f20191130%2f29554138_095901936699_2.jpg&ehk=deyCCJtpHBww4tY9QO9z8ZGsTCZWN%2fdTHjfZ3SYvktU%3d&risl=&pid=ImgRaw&r=0',
						desc: '便捷安全的支付方式'
					}
				]
			}
		},
		computed: {
			// 是否可以点击确认按钮
			canConfirm() {
				if (this.customAmount) {
					return parseFloat(this.customAmount) >= 0.1;
				}
				return this.selectedAmount > 0;
			},
			
			// 实际充值金额
			actualAmount() {
				return this.customAmount ? parseFloat(this.customAmount) : this.selectedAmount;
			},
			userBalance(){
				return user.userInfo.balance || 0
			}
		},
		onLoad() {
			// 获取用户余额信息
			//this.getUserBalance();
		},
		methods: {
			// 获取用户余额信息
			getUserBalance() {
				// 首先尝试从本地存储获取用户信息
				try {
					user.fetchUserInfo()
				} catch (e) {
					console.error('读取缓存用户信息失败', e);
				}
			},
			
			// 选择充值金额
			selectAmount(amount) {
				this.selectedAmount = amount;
				this.customAmount = '';
			},
			
			// 自定义金额输入
			onCustomAmountInput(e) {
				this.customAmount = e.detail.value;
				if (this.customAmount) {
					this.selectedAmount = 0;
				}
			},
			
			// 选择支付方式
			selectPaymentMethod(methodId) {
				this.selectedPaymentMethod = methodId;
			},
			
			// 确认充值
			confirmRecharge() {
				if (!this.canConfirm) {
					return;
				}
				
				const amount = this.actualAmount;
				const payMethod = this.selectedPaymentMethod;
				
				loading('正在发起支付请求...');
				
				// 构建充值参数
				const params = {
					user_money: amount,
					plan_id: 0,
					source: 'app', // 充值来源
				};
				// 调用充值API
				rechargeApi(params).then(res => {
					let order_id = res.order_id
					//发起支付
					payRechargeApi({
						order_id:order_id,
						pay_source:"wx"
					}).then((res)=>{
						let param = res.payment
						//发起支付
						uni.requestPayment({
							provider: "wxpay",
							...param,
							success: function (res) {
								hideloading();
								console.log(res)
							},
							fail: function (err) {
								hideloading();
								tip("支付失败："+JSON.stringify(err))
							},
						})
					})
				}).catch(err => {
					hideloading();
					uni.showToast({
						title: err || '充值失败，请重试',
						icon: 'none',
						duration: 2000
					});
				});
			},
			
			// 刷新余额
			refreshBalance() {
				this.getUserBalance();
				tip('正在刷新余额...');
			}
		}
	}
</script>

<style lang="scss">
	.recharge-container {
		background-color: #F8F6F1;
		min-height: 100vh;
		padding-bottom: 50rpx;
		padding-top: 30rpx;
	}
	
	/* 余额卡片样式 */
	.balance-card {
		margin: 30rpx;
		background: linear-gradient(135deg, #E2C675, #C8A950);
		border-radius: 15rpx;
		padding: 40rpx 30rpx;
		display: flex;
		align-items: center;
		box-shadow: 0 5rpx 15rpx rgba(200, 169, 80, 0.2);
	}
	
	.balance-icon {
		width: 80rpx;
		height: 80rpx;
		border-radius: 40rpx;
		background-color: rgba(255, 255, 255, 0.2);
		display: flex;
		justify-content: center;
		align-items: center;
		margin-right: 20rpx;
	}
	
	.balance-info {
		display: flex;
		flex-direction: column;
		flex: 1;
	}
	
	.balance-label {
		font-size: 28rpx;
		color: rgba(255, 255, 255, 0.9);
	}
	
	.balance-value-wrapper {
		margin-top: 5rpx;
	}
	
	.balance-value {
		font-size: 48rpx;
		color: #fff;
		font-weight: bold;
	}
	
	.balance-loading {
		font-size: 32rpx;
		color: rgba(255, 255, 255, 0.8);
		font-style: italic;
	}
	
	.refresh-btn {
		width: 50rpx;
		height: 50rpx;
		display: flex;
		justify-content: center;
		align-items: center;
	}
	
	/* 标题样式 */
	.section-title {
		font-size: 30rpx;
		color: #4D3F33;
		font-weight: bold;
		margin: 40rpx 30rpx 20rpx;
		font-family: "思源宋体", serif;
	}
	
	/* 金额选择样式 */
	.amount-options {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		padding: 0 20rpx;
	}
	
	.amount-item {
		width: calc(29% - 20rpx);
		margin: 10rpx;
		background-color: #fff;
		border-radius: 10rpx;
		padding: 30rpx 10rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
		border: 1px solid transparent;
		transition: all 0.3s ease;
	}
	
	.amount-item.active {
		border-color: #78A77F;
		background-color: rgba(120, 167, 127, 0.1);
		transform: scale(1.02);
	}
	
	.amount-value {
		font-size: 36rpx;
		color: #333;
		font-weight: bold;
	}
	
	/* 自定义金额样式 */
	.custom-amount {
		margin: 30rpx;
		background-color: #fff;
		border-radius: 15rpx;
		padding: 20rpx;
		box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
	}
	
	.custom-title {
		font-size: 28rpx;
		color: #4D3F33;
		margin-bottom: 20rpx;
	}
	
	.custom-input-wrapper {
		display: flex;
		align-items: center;
		border-bottom: 1px solid #EBEBEB;
		padding-bottom: 10rpx;
	}
	
	.currency-symbol {
		font-size: 36rpx;
		color: #333;
		margin-right: 10rpx;
	}
	
	.custom-input {
		flex: 1;
		height: 60rpx;
		font-size: 32rpx;
	}
	
	/* 支付方式样式 */
	.payment-methods {
		margin: 0 30rpx;
	}
	
	.payment-item {
		display: flex;
		align-items: center;
		background-color: #fff;
		border-radius: 15rpx;
		padding: 25rpx 20rpx;
		margin-bottom: 20rpx;
		box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
		position: relative;
	}
	
	.payment-item.active {
		border-left: 5rpx solid #78A77F;
	}
	
	.payment-icon {
		width: 70rpx;
		height: 70rpx;
		margin-right: 20rpx;
	}
	
	.payment-icon image {
		width: 100%;
		height: 100%;
	}
	
	.payment-info {
		flex: 1;
	}
	
	.payment-name {
		font-size: 30rpx;
		color: #333;
		font-weight: bold;
	}
	
	.payment-desc {
		font-size: 24rpx;
		color: #999;
		margin-top: 8rpx;
	}
	
	.payment-selected {
		width: 40rpx;
		height: 40rpx;
		border-radius: 50%;
		background-color: #78A77F;
		display: flex;
		justify-content: center;
		align-items: center;
	}
	
	/* 充值说明样式 */
	.recharge-notes {
		margin: 40rpx 30rpx;
		background-color: rgba(120, 167, 127, 0.05);
		border-radius: 10rpx;
		padding: 20rpx;
	}
	
	.notes-title {
		font-size: 26rpx;
		color: #4D3F33;
		font-weight: bold;
		margin-bottom: 15rpx;
		display: block;
	}
	
	.notes-item {
		font-size: 24rpx;
		color: #A89B88;
		line-height: 1.6;
		display: block;
	}
	
	/* 确认按钮样式 */
	.confirm-btn-wrapper {
		margin: 50rpx 30rpx 80rpx;
	}
	
	.confirm-btn {
		background: #78A77F;
		color: #fff;
		font-size: 32rpx;
		font-weight: bold;
		height: 90rpx;
		line-height: 90rpx;
		border-radius: 45rpx;
		border: none;
		box-shadow: 0 5rpx 15rpx rgba(120, 167, 127, 0.3);
		transition: all 0.3s ease;
	}
	
	.confirm-btn:active {
		transform: scale(0.98);
		opacity: 0.9;
	}
	
	.confirm-btn.disabled {
		background: #CCCCCC;
		box-shadow: none;
	}
</style>
